package controller;

import beans.Category;
import beans.HttpResultList;
import beans.LogEnum;
import dao.CategoryDao;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import utils.UserLogUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class CategoryServlet extends HttpServlet {
    private CategoryDao categoryDao;
    private HttpResultList result;
    private HttpSession session;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operateType = request.getParameter("operateType");
        session = request.getSession();
        categoryDao = new CategoryDao();
        try {
            switch (operateType){
                case "add":
                    addCategory(request,response);
                    break;
                case "delete":
                    deleteCategory(request,response);
                    break;
                case "alter":
                    alterCategory(request,response);
                    break;
                case "showAll":
                    showAllCategory(request,response);
                    break;
                case "showDetail":
                    showCategoryDetail(request,response);
                    break;
                case "allList":
                    showAllList(request,response);
                    break;
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("POST参数错误");
        } catch (IOException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("服务器错误");
        }catch (NumberFormatException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("POST数字错误");
        }
        response.getWriter().print(JSONObject.fromObject(result).toString());
    }

    private void showAllList(HttpServletRequest request, HttpServletResponse response) {
        result = new HttpResultList();
        List<Object> list = categoryDao.allCategoryList();
        result.setResult(list.size()>0);
        result.setDatas(list);
    }

    private void addCategory(HttpServletRequest request, HttpServletResponse response) throws NullPointerException, IOException {
        String name = request.getParameter("categoryName");
        String isFunction = request.getParameter("isFunction");
        result = new HttpResultList();
        result.setResult(categoryDao.addCategory(name, isFunction));
        // 日志记录
        UserLogUtil.dbLog(LogEnum.category,
                LogEnum.add,
                (String)session.getAttribute("userId"),
                LogEnum.category.getValue()+"名称："+name);
    }
    private void deleteCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String categoryId = request.getParameter("categoryId");
        Category category = (Category) categoryDao.categoryDetail(categoryId).get(0);
        result = new HttpResultList();
        int categoryResult = categoryDao.deleteCategory(categoryId);
        if(categoryResult>0){
            result.setResult(true);
            // 日志记录
            UserLogUtil.dbLog(LogEnum.category,
                    LogEnum.delete,
                    (String)session.getAttribute("userId"),
                    LogEnum.category.getValue()+"编号:"+category.getCategoryId()+"，名称："+category.getCategoryName());
        }else{
            result.setResult(false);
            if(categoryResult==-2){
                result.setMessage("该分类下包含算法，请处理后再删除");
            }else{
                result.setMessage("不存在该分类");
            }
        }
    }
    private void alterCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String categoryId = request.getParameter("categoryId");
        String categoryName = request.getParameter("categoryName");
        String isFunction = request.getParameter("isFunction");
        result = new HttpResultList();
        result.setResult(categoryDao.alterCategory(categoryId, categoryName, isFunction));
        // 日志记录
        UserLogUtil.dbLog(LogEnum.category,
                LogEnum.update,
                (String)session.getAttribute("userId"),
                LogEnum.category.getValue()+"编号："+categoryId);
    }
    private void showAllCategory(HttpServletRequest request, HttpServletResponse response) {
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        result = new HttpResultList();
        List<Object> companyList = categoryDao.showAllCategory(onePageCount, currentPage);
        result.setResult(companyList.size()>0);
        result.setDatas(companyList);
        result.setMessage(categoryDao.categoryPageNum(onePageCount)+"");
    }
    private void showCategoryDetail(HttpServletRequest request, HttpServletResponse response) {
        String companyId = request.getParameter("categoryId");
        result = new HttpResultList();
        List<Object> company = categoryDao.showOneCategory(companyId);
        result.setResult(company.size()>0);
        result.setDatas(company);
    }
}
